import SceneNodeApi from '@/api/ruleNode/sceneNode'
import { addStruct } from '../../components/ruleNode/sceneLib/sceneNode/sceneNode.util'
const state = {
  vehicleSignal: {},
  ecoSignal: {},
  tspSignal: {}
}

const mutations = {
  save: (state, { name, value }) => { state[name] = value }
}

const actions = {
  async getSignal({ commit }, type) {
    if (state[type].options) {
      return state[type]
    } else {
      return await actions.getNewSignal({ commit }, type)
    }
  },
  async getNewSignal({ commit }, type) {
    const { getGacSignals: vehicleSignal, getEcologySignals: ecoSignal, getTspSignals: tspSignal } = SceneNodeApi
    const api = { vehicleSignal, ecoSignal, tspSignal }[type]
    const res = await api(type)
    addStruct(res.data, type) // 缓存格式化好的信号列表以避免卡顿
    commit('save', { name: type, value: res.data })
    return res.data
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
